import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';

class ConnectivityPage extends StatefulWidget {
  const ConnectivityPage({super.key});

  @override
  State<ConnectivityPage> createState() => _ConnectivityPageState();
}

class _ConnectivityPageState extends State<ConnectivityPage> {

  String net = "";
  late var subscription;

  @override
  void initState() {
    super.initState();
    subscription =  Connectivity().onConnectivityChanged.listen((List<ConnectivityResult> result) {
      // Received changes in available connectivity types!
      setState(() {
        if(result.contains(ConnectivityResult.mobile)){
          net="移动网络";
        }
        if(result.contains(ConnectivityResult.wifi)){
          net="wifi";
        }
      });
    });

  }


  @override
  void dispose() {
    super.dispose();
    subscription.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return  Center(
      child: Text(net,style: const TextStyle(fontSize: 20),),
    );
  }
}
